/************************************************************************************
* SongScroller.cpp                                                                  *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "SongScroller.h"
#include <QPainter>

SongScroller::SongScroller(QWidget* parent):
    //This is a comment Mr.Compiler. You are not suppose to read it!!!
    QWidget(parent) {
    m_pixmap = new QPixmap();
    m_startIndex = 0;
    m_timer      = new QTimer(this);
    m_timer->setInterval(40);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(update()));
    m_timer->start();
    m_displayString = "YAMP";
    drawPixmap();
}


SongScroller::~SongScroller() {
    delete m_pixmap;
}


void SongScroller::stop() {
    m_displayString = "YAMP";
    drawPixmap();
}


void SongScroller::setCurrentItem(MediaItem *item) {
    if(m_pixmap != NULL)
    m_displayString = "";
    if(! item->title().isEmpty()) {
        m_displayString += item->title();
    }
    if(! item->artist().isEmpty()) {
        m_displayString += QString::fromUtf8("  \u2022  ")+item->artist();
    }
    if(! item->album().isEmpty()) {
        m_displayString += QString::fromUtf8("  \u2022  ")+item->album();
    }
    drawPixmap();
    update();
}


QSize SongScroller::sizeHint() const{
    return QSize(300, DISPLAY_HEIGHT);
}




void SongScroller::paintEvent(QPaintEvent *event) {

    QPainter scrPainter(this);
//    if(m_startIndex + DISPLAY_LENGTH > m_imageWidth) {
//        QPixmap first = m_pixmap->copy(m_startIndex, 0, (m_imageWidth - m_startIndex), DISPLAY_HEIGHT);
//        scrPainter.drawPixmap(0, 0, first);
//        QPixmap second = m_pixmap->copy(0, 0, (DISPLAY_LENGTH - (m_imageWidth - m_startIndex)), DISPLAY_HEIGHT);
//        scrPainter.drawPixmap((m_imageWidth - m_startIndex), 0, second);
//    }
//    else {
//        QPixmap show = m_pixmap->copy(m_startIndex, 0, DISPLAY_LENGTH, DISPLAY_HEIGHT);
//        scrPainter.drawPixmap(0, 0, show);
//    }
//    m_startIndex = (m_startIndex + 1) % m_imageWidth;

//The above portion is for revolving effect

    if(m_imageWidth <= DISPLAY_LENGTH) {

        scrPainter.drawPixmap(m_startIndex, 0, *m_pixmap);
        if(m_startIndex + m_imageWidth > DISPLAY_LENGTH) {
            m_progression = Backword;
        }
        if (m_startIndex == 0){
            m_progression = Forword;
        }
    }
    else {
        QPixmap show = m_pixmap->copy(m_startIndex, 0, DISPLAY_LENGTH, DISPLAY_HEIGHT);
        scrPainter.drawPixmap(0, 0, show);
        if(m_startIndex + DISPLAY_LENGTH > (m_imageWidth)) {
            m_progression = Backword;
        }
        if(m_startIndex == 0) {
            m_progression = Forword;
        }
    }
    m_startIndex = (m_progression == Forword? m_startIndex + 1 : m_startIndex - 1);
}




void SongScroller::drawPixmap() {

    delete m_pixmap;

    m_pixmap = new QPixmap((m_displayString.size() * 16), DISPLAY_HEIGHT);//m_imageWidth, DISPLAY_HEIGHT);
    m_pixmap->fill(qRgba(0,0,0,0));
    QPixmap copy = m_pixmap->copy(m_pixmap->rect());
    m_pixmap->setAlphaChannel(copy);

    QPainter pxPainter(m_pixmap);
    pxPainter.setFont(QFont(this->font().defaultFamily(), 9));
    pxPainter.setPen(QColor(Qt::green));                  //<<Set>>
    pxPainter.drawText(X_OFFSET, 20, m_displayString);

    QFontMetrics metric = pxPainter.fontMetrics();
    m_imageWidth = metric.width(m_displayString) + X_OFFSET * 2;
    m_progression = Forword;
}
